﻿<?php
/**
 * 957796988@qq.com
 */
ini_set('implicit_flush',1);
ob_implicit_flush(1);
ob_end_clean();
date_default_timezone_set('PRC');
set_time_limit(0);

class GetImg{

	private 	$start_page=90010;
	private		$end_page=91000;
	private 	$url='images';

	public function set($key,$val){

		$this->$key=$val;
	}

	// private function curl_get_con($url,$https=true,$method="get",$data=null){
	// 	$ch=curl_init();

	// 	curl_setopt($ch,CURLOPT_URL,$url);

	// 	curl_setopt($ch,CURLOPT_HEADER,false);

	// 	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

	// 	if($https){
	// 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

	// 		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
	// 	}

	// 	if($method=="post"){
	// 		curl_setopt($ch,CURLOPT_POST,true);

	// 		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	// 	}

	// 	$str=curl_exec($ch);

	// 	curl_close($ch);
	// 	return $str;
	// }

	// private function save_img($url,$dir,$i){

	// 	if(!is_dir($dir)){
	// 		mkdir($dir,777);
	// 	}
	// 	$res=file_get_contents($url);
	// 	$file_name=$dir.'/'.$i.'_'.date('Ymd_His').'.jpg';
	// 	file_put_contents($file_name,$res);
	// 	return $file_name;
	// }
	public $urls = [];

	private function curl_get_con($url,$https=true,$method="get",$data=null){

		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);

		curl_setopt($ch,CURLOPT_HEADER,false);

		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

		if($https){
			curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

			curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		}

		if($method=="post"){
			curl_setopt($ch,CURLOPT_POST,true);

			curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		}

		$str=curl_exec($ch);
		$staue = curl_getinfo($ch,CURLINFO_HTTP_CODE);
		if ($staue!=200) {
			return false;
		}
		
		
		$this->urls[] = $url;
		echo '<tr><td>'.$url.'</td>--------<td>'.$staue.'</td></tr>';
		
		
		curl_close($ch);
		// return $str;
		preg_match_all("/http:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$str,$a);
		$a=$a[0];
			// echo "<tr><td>正在捕捉".$i."。</td><td>";
// echo "<pre>";print_r($a);exit();
		if(empty($a)){

			return false;
		}
			$bb = true;
			foreach ($a as $value){
				foreach ($this->urls as $v) {
					if ($v==$value)
						$bb = false;

				}
				if ($bb) 
					$this->curl_get_con($value);
				 // $file_name=$this->save_img($value,$this->url,$i);
			}

	}




	public function start_getimg(){

		echo "<style>table{ border-collapse:collapse }tr{border:1px solid #ccc}</style>
		<table width='1000' border='0' cellpadding='5' cellspacing='5' ><tr width='100'><td colspan='2'>爬虫已运行：</td></tr>";
		// for($i=$this->start_page+1;$i<=$this->end_page;$i++){

			$url="https://www.toutiao.com/";

			$result=$this->curl_get_con($url);


			// preg_match_all("/http:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$result,$a);
			// preg_match_all("/http:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$result,$a);

			// $a=$a[0];
			// // echo "<tr><td>正在捕捉".$i."。</td><td>";

			// if(empty($a)){

			// 	echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;----空。</td></tr>";
			// 	continue;
			// }

			// foreach ($a as $value){
				
			// 	 // $file_name=$this->save_img($value,$this->url,$i);
			// 	 echo $value;

			// }
			// if($i==$this->end_page){
			// 	echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;----".$i."捕捉结束。</td></tr>";
			// 	echo "<br />全部捕捉结束。";
			// 	break;
			// }
			echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;----捕捉结束。</td></tr>";

		// }
	}
}

?>
